package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.CRMPlatformServiceDao;
import com.cci.kangdao.dao.model.APIExceptionT;
import com.cci.kangdao.service.CRMLogService;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author William Du
 * @date 2019/7/30
 */
@Service
public class CRMLogServiceImpl implements CRMLogService {

    @Resource
    private CRMPlatformServiceDao crmPlatformServiceDao;

    private void saveLog(String serviceChannel,String apiName, String dataJson,String exceptionInfo,Integer direction) {
        APIExceptionT crmApiException = new APIExceptionT();
        crmApiException.setAPIName(apiName);
        crmApiException.setCompanyNo(serviceChannel);
        crmApiException.setData(dataJson);
        crmApiException.setDirection(direction);
        crmApiException.setExceptionInfo(exceptionInfo);
        crmApiException.setFlag(0);
        crmApiException.setSystemTime(LocalTimeUtil.getRealCurrentTime());
        crmPlatformServiceDao.saveExceptionToExpTable(crmApiException);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveCrmErrorLog(String serviceChannel, String apiName, String dataJson, String exceptionInfo) {
        this.saveLog(serviceChannel,apiName,dataJson,exceptionInfo,1);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveAppErrorLog(String serviceChannel, String apiName, String dataJson, String exceptionInfo) {
        this.saveLog(serviceChannel,apiName,dataJson,exceptionInfo,0);
    }
}
